package com.lenovo.vod.player.setting.util;

import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lenovo.vod.player.R;

public class MediaOptionArrayAdapter extends BaseAdapter{

	private Context mContext;
	private List<String> mediaOptions;
	private boolean mIsCollection;
	private String mCollectionStr;
	private String mCancelCollectionStr;
	
	public MediaOptionArrayAdapter(Context context,  String[] objects,String mCoString,String mCanString) {
        this.mContext = context;
        this.mediaOptions = Arrays.asList(objects);
        this.mCancelCollectionStr = mCanString;
		this.mCollectionStr = mCoString;
	}
	
	public MediaOptionArrayAdapter(Context context){
		this.mContext = context;
	}
	
	public MediaOptionArrayAdapter(List<String> mediaOptions , Context context,boolean isCollection,String mCoString,String mCanString ){
		this.mContext = context;
		this.mediaOptions = mediaOptions;
		this.mIsCollection = isCollection;
		this.mCollectionStr = mCoString;
		this.mCancelCollectionStr = mCanString;
		this.mCollectionStr = mCoString;
	}
	public void setCollection(boolean isCollection){
		this.mIsCollection = isCollection;
		this.notifyDataSetChanged();
	}
	
	public void changeData(List<String> mediaOptions){
		this.mediaOptions = mediaOptions;
		this.notifyDataSetChanged();
	}
	
	public void changeData(int textArrayRecourseId,String mCoString,String mCanString){
		String[] strArr = mContext.getResources().getStringArray(textArrayRecourseId);
		this.mediaOptions = Arrays.asList(strArr);
		this.mCancelCollectionStr = mCanString;
		this.mCollectionStr = mCoString;
		
		// call getView() again to re-paint the View displaying the new data 
		this.notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mediaOptions.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return mediaOptions.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int resource, View convertView, ViewGroup parent) {
		View view;

		if (convertView == null) {
			view = LayoutInflater.from(mContext).inflate(R.layout.vodplayer_setting_option_listitems, parent, false);
		} else {
			view = convertView;
		}
		
		String mOptionName = mediaOptions.get(resource);
		
		if(mOptionName != null && mCollectionStr.equals(mOptionName)){
			if(mIsCollection){
				mOptionName = mCancelCollectionStr;
			}else{
				mOptionName = mCollectionStr;
			}
			
		}
		TextView text = (TextView) view.findViewById(R.id.vodplayer_setting_tvOption);
		text.setText(mOptionName);
		
		return view;
	}
	
	public static MediaOptionArrayAdapter createFromResource(
			Context context, int textArrayResId ,String mCoString,String mCanString) {
		String[] strings = context.getResources().getStringArray(
				textArrayResId);
		return new MediaOptionArrayAdapter(context,
				strings,mCoString,mCanString);
	}

}
